<template>
  <el-dialog title="积分操作" :close-on-click-modal="false" :visible.sync="visible" class="cu-dialog cu-dialog_center"
    lock-scroll width="900px">
    <el-row :gutter="15" class="">
      <el-form ref="elForm" :model="dataForm" size="small" label-width="100px" label-position="right" :rules="rules">
        <el-col :span="12">
          <el-form-item label="积分规则" prop="ruleId">
            <el-select v-model="dataForm.ruleId" placeholder="请选择" filterable clearable required
              :style='{"width":"100%"}' @change="hanldRuleChange($event)">
              <el-option v-for="(item, index) in ruleOptions" :key="index" :label="item.fullName+'('+item.points+')'"
                :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="积分值" prop="points">
            <el-input v-model="dataForm.points" :style='{"width":"100%"}' readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="memo">
            <el-input v-model="dataForm.memo" placeholder="请输入" show-word-limit :style='{"width":"100%"}'
              type='textarea' :autosize='{"minRows":4,"maxRows":4}'>
            </el-input>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="loading">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import request from '@/utils/request'
export default {
  components: {},
  props: [],
  data () {
    return {
      loading: false,
      visible: false,
      dataForm: {
        id: undefined,
        ruleId: undefined,
        ruleTitle: undefined,
        points: undefined,
        memo: undefined,
        type: 'Artificial'
      },
      rules: {
        ruleId: [
          {
            required: true,
            message: '请选择规则',
            trigger: 'change'
          },
        ],
        points: [
          {
            required: true,
            message: '请输入积分值',
            trigger: 'blur'
          },
        ],
      },
      ruleOptions: []
    }

  },
  computed: {},
  watch: {},
  created () {
  },
  mounted () {
    request({
      url: `/api/campus/integral/rule/selector`,
      method: 'get'
    }).then(res => {
      this.ruleOptions = res.data.filter(f => f.type == 'Artificial');
    })
  },
  methods: {
    hanldRuleChange (value) {
      const rule = this.ruleOptions.find(
        (ele) => ele.id === value
      );
      this.dataForm.points = rule.points
      this.dataForm.ruleTitle = rule.fullName
    },
    init (id) {
      this.dataForm.id = id || '';
      this.visible = true;
      this.$nextTick(() => {
        this.$refs['elForm'].resetFields();
      })
    },
    dataFormSubmit () {
      this.loading = true;
      this.$refs['elForm'].validate((valid) => {
        if (valid) {
          request({
            url: `/api/campus/integral`,
            method: 'post',
            data: this.dataForm,
          }).then((res) => {
            this.$message({
              message: res.msg,
              type: 'success',
              duration: 1000,
              onClose: () => {
                this.visible = false
                this.loading = false;
                this.$emit('refresh', true)
              }
            })
          }).catch((err) => {
            console.log(err)
            this.loading = false;
          })
        }
      })
    },
  }
}
</script>
